package com.wenhui.schoolyardinfo.firstpage.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnItemItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnSecondThirdViewClickListener;
import com.wenhui.schoolyardinfo.firstpage.bean.CommentBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/14 11:06
 * introduce : 评论Adapter
 */

public class CommentsAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<CommentBean> commentBeanList = new ArrayList<>();
    private SubcommentsAdapter subcommentsAdapter;
    private LinearLayoutManager layoutManager;

    public CommentsAdapter(Context context) {
        this.context = context;
    }

    public CommentsAdapter(Context context, List<CommentBean> commentBeanList) {
        this.context = context;
        this.commentBeanList = commentBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment, null);
        return new ContentHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.tevName.setText(commentBeanList.get(position).getName() + ":");
            contentHolder.tevComment.setText(commentBeanList.get(position).getComment());
            layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(OrientationHelper.VERTICAL);
            contentHolder.subcommentsRecyclerView.setLayoutManager(layoutManager);
            contentHolder.subcommentsRecyclerView.setItemAnimator(new DefaultItemAnimator());
            subcommentsAdapter = new SubcommentsAdapter(context, commentBeanList.get(position).getSubcommentsBeanList());
            contentHolder.subcommentsRecyclerView.setAdapter(subcommentsAdapter);
            subcommentsAdapter.setRcvOnItemItemItemViewClickListener(new RcvOnItemItemItemViewClickListener() {
                @Override
                public void onItemItemItemClickListener(int position3, View view) {
                    rcvOnSecondThirdViewClickListener.onItemItemItemClickListener(position, position3, view);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return commentBeanList.size();
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_comment)
        TextView tevComment;
        @BindView(R.id.subcomments_recycler_view)
        RecyclerView subcommentsRecyclerView;
        @BindView(R.id.imv_spread)
        ImageView imvSpread;
        @BindView(R.id.layout_spread)
        RelativeLayout layoutSpread;

        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            tevName.setOnClickListener(this);
            tevComment.setOnClickListener(this);
            layoutSpread.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tev_name:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.tev_comment:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_spread:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemItemViewClickListener rcvOnItemItemViewClickListener;

    public void setRcvOnItemItemViewClickListener(RcvOnItemItemViewClickListener rcvOnItemItemViewClickListener) {
        this.rcvOnItemItemViewClickListener = rcvOnItemItemViewClickListener;
    }

    private RcvOnSecondThirdViewClickListener rcvOnSecondThirdViewClickListener;

    public void setRcvOnSecondThirdViewClickListener(RcvOnSecondThirdViewClickListener rcvOnSecondThirdViewClickListener) {
        this.rcvOnSecondThirdViewClickListener = rcvOnSecondThirdViewClickListener;
    }
}
